//
//  ViewController.m
//  ZeroWorld
//
//  Created by 马进 on 2018/2/9.
//  Copyright © 2018年 马进. All rights reserved.
//

#import "ViewController.h"
#import "MainGame.h"
@interface ViewController ()<MainRoleDelegate>
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) NSMutableString *displayTexts;
@property (nonatomic, strong) MainGame *game;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.displayTexts = [[NSMutableString alloc] initWithString:@"游戏开始...\n"];
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, self.view.frame.size.width - 40, 300)];
    self.textView.text = self.displayTexts;
    self.textView.backgroundColor = [UIColor greenColor];

    [self.view addSubview:self.textView];
    MainGame *game = [[MainGame alloc] init];
    game.mainRole.delegate = self;
    [game start];
    self.game = game;
    
    //方向控制
    UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.center.x - 25, self.textView.frame.origin.y + self.textView.frame.size.height + 50, 50, 50)];
    [btn1 setTitle:@"北" forState:UIControlStateNormal];
    btn1.backgroundColor = [UIColor lightGrayColor];
    [btn1 addTarget:self action:@selector(onTouchButton:) forControlEvents:UIControlEventTouchUpInside];
    btn1.tag = MapDirectionNorth;
    [self.view addSubview:btn1];
    
    UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.center.x - 25, btn1.frame.origin.y + btn1.frame.size.height + 50, 50, 50)];
    [btn2 setTitle:@"南" forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(onTouchButton:) forControlEvents:UIControlEventTouchUpInside];
    btn2.backgroundColor = [UIColor lightGrayColor];
    btn2.tag = MapDirectionSouth;
    [self.view addSubview:btn2];
    
    UIButton *btn3 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.center.x - 25 - 50, btn1.frame.origin.y + btn1.frame.size.height + 0, 50, 50)];
    [btn3 setTitle:@"东" forState:UIControlStateNormal];
    [btn3 addTarget:self action:@selector(onTouchButton:) forControlEvents:UIControlEventTouchUpInside];
    btn3.backgroundColor = [UIColor lightGrayColor];
    btn3.tag = MapDirectionEast;
    [self.view addSubview:btn3];
    
    UIButton *btn4 = [[UIButton alloc] initWithFrame:CGRectMake(self.view.center.x + 25, btn1.frame.origin.y + btn1.frame.size.height + 0, 50, 50)];
    [btn4 setTitle:@"西" forState:UIControlStateNormal];
    [btn4 addTarget:self action:@selector(onTouchButton:) forControlEvents:UIControlEventTouchUpInside];
    btn4.backgroundColor = [UIColor lightGrayColor];
    btn4.tag = MapDirectionWest;
    [self.view addSubview:btn4];
}

- (void)mainRoleTextOutPut:(NSString *)outStrings {
    [self.displayTexts appendFormat:@"\n%@", outStrings];
    self.textView.text = self.displayTexts;
    CGFloat y = self.textView.contentSize.height - self.textView.frame.size.height;
    self.textView.contentOffset = CGPointMake(0, y);
}

- (void)onTouchButton:(UIButton *)button {
    [self.game.mainRole move:button.tag];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
